package com.controller;

import java.util.Arrays;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.entity.LessonEntity;
import com.utils.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;
import com.annotation.SysLog;

import com.entity.ChapterEntity;
import com.entity.view.ChapterView;
import com.service.ChapterService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MPUtil;

/**
 * 课程章节
 * 后端接口
 */
@RestController
@RequestMapping("/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    /**
     * 按页查询和模糊搜索功能
     * */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, ChapterEntity chapter,
                  HttpServletRequest request){

        EntityWrapper<ChapterEntity> ew = new EntityWrapper<ChapterEntity>();

        if (params.get("courseId") != null) {
            ew.eq("course_id", params.get("courseId"));
        }

        PageUtils page = chapterService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, chapter), params), params));

        return R.ok().put("data", page);
    }


    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        ChapterEntity chapter = chapterService.selectById(id);
        return R.ok().put("data", chapter);
    }

    /**
     * 新增章节
     * */
    @RequestMapping("/save")
    @SysLog("新增课程章节")
    public R save(@RequestBody ChapterEntity chapter, HttpServletRequest request){
        if (chapter.getCourseId() == null) {
            return R.error("课程ID不能为空");
        }
        Integer maxChapterOrder = chapterService.getMaxChapterOrderByCourseId(chapter.getCourseId());

        chapter.setChapterOrder(maxChapterOrder + 1);

        chapterService.insert(chapter);

        return R.ok();
    }


    /**
     * 教师新增章节
     * */
    @RequestMapping("/teacherAdd")
    @Transactional
    public R teacherAdd(@RequestParam("courseId") Long courseId,
                        @RequestParam("title") String title,
                        HttpServletRequest request) {
        ChapterEntity chapter = new ChapterEntity();
        chapter.setTitle(title);
        chapter.setCourseId(courseId);

        Integer maxChapterOrder = chapterService.getMaxChapterOrderByCourseId(courseId);

        chapter.setChapterOrder(maxChapterOrder + 1);

        chapterService.insert(chapter);
        return R.ok();
    }

    /**
     * 修改章节
     * */
    @RequestMapping("/update")
    @Transactional
    @SysLog("修改课程章节")
    public R update(@RequestBody ChapterEntity chapter, HttpServletRequest request){
        chapterService.updateById(chapter);

        return R.ok();
    }


    /**
     * 教师修改章节
     * */
    @RequestMapping("/teacherUpdate")
    @Transactional
    @SysLog("修改课程章节")
    public R teacherUpdate(@RequestParam("chapterId") Long chapterId,
                        @RequestParam("title") String title,
                        HttpServletRequest request) {
        ChapterEntity chapter = chapterService.selectById(chapterId);
        chapter.setTitle(title);
        chapterService.updateById(chapter);
        return R.ok();
    }

    /**
     * 删除章节
     * */
    @RequestMapping("/delete")
    @SysLog("删除课程章节")
    public R delete(@RequestParam Long id){
        chapterService.deleteById(id);
        return R.ok();
    }
}
